<?php

require_once './lib/phpexcel/Classes/PHPExcel.php';

$reader = PHPExcel_IOFactory::createReader('Excel5');
$excel = $reader->load('./files/templates/Penerima_Beasiswa.xls');
$excel->getProperties()->setCreator("SIMBION")
						->setLastModifiedBy("SIMBION")
						->setTitle("Laporan Penerima Beasiswa")
						->setSubject("Laporan Penerima Beasiswa")
						->setDescription("Laporan Penerima Beasiswa Periode ".$start." - ".$end)
						->setCategory("Penerima Beasiswa");

$baseSheet = $excel->getSheet();
$baseSheet->setTitle($start);
$ind = $start + 1;

while ($ind <= $end) {
	$sheetnext = clone $baseSheet;
	$sheetnext->setTitle("$ind");
	$excel->addSheet($sheetnext);
	$ind = $ind + 1;
}

foreach ($data as $key => $recievers) {
	$excel->setActiveSheetIndexByName($key);
	$baseRow = 4;
	if ($recievers == NULL) {
		
	} else {
		foreach ($recievers as $i => $reciever) {
			$row = $baseRow + $i;
			$excel->getActiveSheet()->insertNewRowBefore($row, 1);
			
			$excel->getActiveSheet()->setCellValue('A'.$row, $i+1);
			$excel->getActiveSheet()->getStyle('A'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$excel->getActiveSheet()->getStyle('A'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->setCellValue('B'.$row, "'".$reciever['number']);
			$excel->getActiveSheet()->getStyle('B'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getStyle('B'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->setCellValue('C'.$row, $reciever['student']);
			$excel->getActiveSheet()->getStyle('C'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$excel->getActiveSheet()->getStyle('C'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->setCellValue('D'.$row, $reciever['major']);
			$excel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getStyle('D'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->setCellValue('E'.$row, $reciever['name']);
			$excel->getActiveSheet()->getStyle('E'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
			$excel->getActiveSheet()->getStyle('E'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->setCellValue('F'.$row, date(MONTH_YEAR_FORMAT, strtotime($reciever['period_start'])));
			$excel->getActiveSheet()->getStyle('F'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getStyle('F'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->setCellValue('G'.$row, date(MONTH_YEAR_FORMAT, strtotime($reciever['period_end'])));
			$excel->getActiveSheet()->getStyle('G'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			$excel->getActiveSheet()->getStyle('G'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->setCellValue('H'.$row, $reciever['details']);
			$excel->getActiveSheet()->getStyle('H'.$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
																		->setWrapText(true);
			$excel->getActiveSheet()->getStyle('H'.$row)->getFont()->setBold(false);
			
			$excel->getActiveSheet()->getRowDimension($row)->setRowHeight(-1);
		}
	}
}

$excel->setActiveSheetIndex(0);

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Penerima_Beasiswa_Periode_'.$start.'_-_'.$end.'.xls"');
header('Cache-Control: max-age=0');

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$writer->save('php://output');
exit;

